package com.umeng.community.example.Utils;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.umeng.comm.core.imageloader.ImgDisplayOption;
import com.umeng.comm.core.sdkmanager.ImageLoaderManager;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public final class BigImgUtils {

	private BigImgUtils() {
	}

	public static AlertDialog alert(final Context context, int messagId) {
		return alert(context, messagId, null);
	}

	public static AlertDialog alert(final Context context, String message) {
		return alertView(context, null, null, message,
				context.getString(R.string.confirm_level), null, null, false,
				null);
	}

	public static AlertDialog alert(final Context context, int messagId,
			DialogInterface.OnClickListener onConfirmClickListener) {
		return alertView(context, null, null, context.getString(messagId),
				context.getString(R.string.confirm_level),
				context.getString(R.string.cancel_level),
				onConfirmClickListener, false, null);
	}

	public static AlertDialog alertView(Context context, View view,
			String title, String message, String confirm, String cancel,
			DialogInterface.OnClickListener onConfirmClickListener,
			boolean negavite,
			final DialogInterface.OnClickListener onCancelClickListener) {

		if (context instanceof Activity && ((Activity) context).isFinishing()) {
			return null;
		}

		final Builder builder = new Builder(context);

		if (!TextUtils.isEmpty(title)) {
			builder.setTitle(title);
		}

		if (view != null) {
			builder.setView(view);
		}

		if (!TextUtils.isEmpty(message)) {
			builder.setMessage(message);
		}

		builder.setPositiveButton(confirm, onConfirmClickListener);

		if (negavite) {
			builder.setNegativeButton(cancel, onCancelClickListener);
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					if (onCancelClickListener != null) {
						onCancelClickListener.onClick(dialog, 0);
					}
				}
			});
		}

		final AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	private static AlertDialog al;

	/**
	 *
	 * @param mContext
	 * @param titleStringId
	 * @param onConfirmClickListener
	 * @return
	 */
	public static AlertDialog showDialog(final Context mContext,
			int titleStringId, OnClickListener onConfirmClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 指定单位，设置字体大小

		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				al.cancel();
			}
		});
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	// 确认 和 取消 都回调
	public static AlertDialog showDialog(final Context mContext,
			int titleStringId, OnClickListener onConfirmClickListener,
			OnClickListener onCancelClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new Builder(mContext).create();
		al.setCancelable(true);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 指定单位，设置字体大小

		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(onCancelClickListener);

		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}


	public static AlertDialog showDialog(final Context mContext,String content) {

		if (al != null) {
			al.cancel();
		}
		al = new Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = getWidth(mContext); // 宽度
		lp.height = getHigth(mContext);
		window.setAttributes(lp);
		window.setContentView(R.layout.bigimg_dialog);
		ImageView img = (ImageView) window.findViewById(R.id.imgview);
//		Bitmap newBitmap = zoomImg(getLoacalBitmap(content), mContext);
//		img.setImageBitmap(newBitmap);

		ImgDisplayOption option = ImgDisplayOption.getCommonDisplayOption();
		option.requestOrigin = false;
		ImageLoaderManager.getInstance().getCurrentSDK().displayImage(
				content, img, option);
		img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				al.cancel();
			}
		});

//		alertTitle.setText(content);

		return al;
	}

	/**
	 * 根据image设置宽高。如果是wrap_content,match_parent则返回宽高250</br>
	 *
	 * @param imageView
	 * @return
	 */
	private Point getSize(ImageView imageView) {
		Point size = new Point();
		if (imageView.getWidth() > 0) {
			size.x = imageView.getWidth();
			size.y = imageView.getHeight();
		} else {
			size.x = size.y = 250;
		}
		return size;
	}
	/**
	 *  处理图片
	 * @param bm 所要转换的bitmap
	 * @param mContext
	 * @return 指定宽高的bitmap
	 */
	public static Bitmap zoomImg(Bitmap bm,Context mContext){
		// 获得图片的宽高
		int width = bm.getWidth();
		int height = bm.getHeight();
		int newWidth = getWidth(mContext);
		int newHeight = newWidth*height/width;
		// 计算缩放比例
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片   www.2cto.com
		Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
		return newbm;
	}
	/**
	 * 加载本地图片
	 * @param url
	 * @return
	 */
	public static Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			BitmapFactory.Options options = new  BitmapFactory.Options();

			options.inJustDecodeBounds =  false;

			options.inSampleSize =  10;   // width，hight设为原来的十分一


			Bitmap bm = BitmapFactory.decodeStream(fis,null,options);
			return bm;  ///把流转化为Bitmap图片

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	/***
	 * 
	 * 获取屏幕宽度
	 */
	public static int getWidth(Context mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
		// int screenHeigh = dm.heightPixels;
	}


	/***
	 *
	 * 获取屏幕宽度
	 */
	public static int getHigth(Context mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.heightPixels;
		// int screenHeigh = dm.heightPixels;
	}
}
